package com.ksp.wke.kspu01;
import static com.ksp.com.KSPCOM_BasConst.*;
import static com.ksp.com.KSPCOM_BasConst.SES_TKN;
import static com.ksp.com.KSPCOM_BasConst.USR_OAM;
import static com.ksp.com.KSPCOM_BasConst.expire;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksp.api.KSPAPI_CookieUtil;
import com.ksp.dom.KSPDOM_ResDta;
import com.ksp.dom.KSPDOM_User;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;


@Controller
public class KSPU01Controller {

	@Autowired
	private KSPU01Mapper kspu01Mapper;
	
	@Resource
	private CacheManager cacheManager;

	@RequestMapping("/usr/KSPU01")
	@ResponseBody
	public KSPDOM_ResDta iniPam(ServletRequest request, HttpServletResponse response) {
		HttpServletRequest hreq = (HttpServletRequest)request;
		String wUsrOam = hreq.getParameter("USR_OAM");
		String wPasWrd = hreq.getParameter("PAS_WRD");
		Map<String, Object> wPamMap = new HashMap<>();
		wPamMap.put("USR_OAM", wUsrOam);
		wPamMap.put("PAS_WRD", wPasWrd);
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		try {
			wResDta = exeSrv(wPamMap);
		} catch (Exception e) {
			wResDta.setCod("400");
			wResDta.setMsg(e.getMessage());
		}
		KSPDOM_User curUsr = new KSPDOM_User();
		Cache userCache = cacheManager.getCache("userCache");
		//用户登录验证
		if ("200".equals(wResDta.getCod())) {
			String token = UUID.randomUUID().toString();
			String wUsrInfStr = wResDta.getJdt();
			JSONObject jUsrInf = (JSONObject) JSONValue.parse(wUsrInfStr);
			String wjUsrNam = jUsrInf.getAsString("usrnam");
			String wjPasWrd = jUsrInf.getAsString("paswrd");
			String wjUsrTyp = jUsrInf.getAsString("usrtyp");
			String wjUsrOam = jUsrInf.getAsString("usroam");
			
			curUsr = new KSPDOM_User(
					wjUsrNam,
					wjPasWrd,
					wjUsrTyp,
					wjUsrOam);
			hreq.getSession().setAttribute(SES_TKN + ":" + token, wUsrOam);
			hreq.getSession().setAttribute(USR_OAM, curUsr);
			KSPAPI_CookieUtil.addCookie(response, COO_TKN, token, expire);
			//加入ehcache
			userCache.put(EHK_USR_INF, curUsr);
			System.out.println("用户信息已经缓存");
		}
		
		
		KSPDOM_User ehkUsr = userCache.get(EHK_USR_INF, KSPDOM_User.class);
		if (ehkUsr != null) {
			System.out.println("用户信息已经缓存，提取" + ehkUsr.getUsroam());
			
		}
		
		
		wResDta.setJdt("");
		return wResDta;
	}

	public KSPDOM_ResDta exeSrv(Map<String, Object> pPamMap) {
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		wResDta.setCod("400");
		List<Map<String, Object>> wQryPwdLst = kspu01Mapper.qryUsrPasword(pPamMap);
		if (wQryPwdLst.size() > 0) {
			Map<String, Object> wQryPwdMap = wQryPwdLst.get(0);
			if (wQryPwdMap != null) {
				String wUsrNam = (String)wQryPwdMap.get("USR_NAM");
				String wPasWrd = (String)wQryPwdMap.get("PAS_WRD");
				String wUsrTyp = (String)wQryPwdMap.get("USR_TYP");
				String wUsrOam = (String)wQryPwdMap.get("USR_OAM");
				
				String wInpPwd = (String)pPamMap.get("PAS_WRD");
				if (wInpPwd.equals(wPasWrd)) {
					wResDta.setCod("200");
					wResDta.setMsg("登录成功");
					KSPDOM_User usr = new KSPDOM_User(wUsrNam, wPasWrd, wUsrTyp, wUsrOam);
					// 通用的map  转 json字符串
					// java实体类   转 json字符串
					wResDta.setJdt(JSONValue.toJSONString(usr));
					String jsonString = JSONValue.toJSONString(wQryPwdMap);
					
					//json字符串 转 java实体类
					String jdt = wResDta.getJdt();
					
					//KSPDOM_User usr2 = (KSPDOM_User)JSONValue.parse(jdt);
					//字符串转 json数组
					//JSONArray jArr = (JSONArray)jsonobject;
					
				} else {
					wResDta.setMsg("用户密码错误");
				}
			} else {
				wResDta.setMsg("用户不存在，请先注册");
			}
		} else {
			wResDta.setMsg("用户不存在，请先注册");
		}
		
		return wResDta;
	}
}


/**
 * json实用工具net.minidev.json
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 * 
 */
